package cms.simmytech.service;

import cms.simmytech.mapper.TbUserMajorMapper;
import cms.simmytech.mapper.TbUserMapper;
import cms.simmytech.mapper.TbUserWorkMapper;
import cms.simmytech.model.*;
import cms.simmytech.response.BaseResponse;
import cms.simmytech.response.MomentDetailResponse;
import cms.simmytech.response.UserDetailResponse;
import cms.simmytech.util.DateUtil;
import cms.simmytech.util.ResultCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TbUserService {
    @Autowired
    private TbUserMapper tbUserMapper;
    @Autowired
    private CommonService commonService;
    @Autowired
    private TbUserMajorMapper userMajorMapper;
    @Autowired
    private TbUserWorkMapper userWorkMapper;

    public PageInfo<TbUser> getTbUserList(int pageNum, int pageSize, String name, Byte userGender, Byte   userState) {
        PageHelper.startPage(pageNum, pageSize);
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        if (!"".equals(name) && null != name) {
            criteria.andUserNameLike("%" + name + "%");
        }
        if (userGender != null) {
            criteria.andUserGenderEqualTo(userGender);
        } if (userState != null) {
            criteria.andUserStateEqualTo(userState);
        }
        List<TbUser> list = tbUserMapper.selectByExample(example);
        PageInfo<TbUser> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    public void delete(int id) {
        tbUserMapper.deleteByPrimaryKey(id);
    }

    public List<TbUser> getAllList() {
        TbUserExample example = new TbUserExample();
        return tbUserMapper.selectByExample(example);
    }

    //	通过主键查询id
    public TbUser findTbUserById(Integer TbUserId) {
        return tbUserMapper.selectByPrimaryKey(TbUserId);

    }

//    //根据id查询一个用户的信息，
//    public TbUser findTbUserById(Integer TbUserId){
//        TbUser result=new TbUser();
//        try{
//            result=mapper.selectByPrimaryKey(TbUserId);
//        }catch(Exception e){
//            e.printStackTrace();
//        }
//        return result;
//    }


    ////	tbuser删除
//	public void deleteUser(int id) {
//        tbUserMapper.deleteByPrimaryKey(id);
//	}
    //新增或者修改专业信息
    public void saveTbUser(TbUser tbUser) {
        if (tbUser.getUserId() == null) {
            tbUserMapper.insertSelective(tbUser);
        } else {
            tbUserMapper.updateByPrimaryKeySelective(tbUser);
        }
    }

    public UserDetailResponse getUserDetail(Integer id) {
        UserDetailResponse response = new UserDetailResponse(ResultCode.OPERATION_ERROR);
        TbUser user = tbUserMapper.selectByPrimaryKey(id);
        response.getData().setUser(user);
        TbUserMajorExample userMajorExample=new TbUserMajorExample();
        userMajorExample.createCriteria().andUserIdEqualTo(id).andMajorStateEqualTo((byte) 1);
        List<TbUserMajor> usermajorList = userMajorMapper.selectByExample(userMajorExample);
        TbUserWorkExample userWorkExample=new TbUserWorkExample();
        userWorkExample.createCriteria().andUserIdEqualTo(id);
        List<TbUserWork> userWorkList = userWorkMapper.selectByExample(userWorkExample);
        response.getData().setUserWorkList(userWorkList);
        response.getData().setUserMajorList(usermajorList);
        return  response;

    }

    public BaseResponse updateUser(Integer uId, Byte state) {
        BaseResponse  response=new BaseResponse(ResultCode.OPERATION_ERROR);
        TbUser bean = tbUserMapper.selectByPrimaryKey(uId);
        if (null == bean) {
            response.setResponse(ResultCode.USER_NO_EXIST);
            return response;
        }
        bean.setUserState(state);
            if(bean.getUserState()==1){
                BaseResponse res = commonService.generateUserCode(bean);

            }
        tbUserMapper.updateByPrimaryKeySelective(bean);
        response.setResponse(ResultCode.SUCCESS);
        return response;
    }
}
